package br.odb.bzk3.android;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import javax.microedition.khronos.opengles.GL10;


//import android.opengl.GLES20;
import br.odb.libscene.IndexedSetFace;
import br.odb.libstrip.AbstractTriangle;

public class Triangle implements IndexedSetFace, AbstractTriangle {

	public float x0;
	public float y0;
	public float x1;
	public float y1;
	public float x2;
	public float y2;
	public float z0;
	public float z1;
	public float z2;
	public float r;
	public float g;
	public float b;
	public float a;
	private FloatBuffer vertexBuffer;
	private FloatBuffer colorBuffer;
	private float vertices[];

	private float[] color;

	private float offset;

	// ------------------------------------------------------------------------------------------------------------
	public Triangle() {
		vertices = new float[9];
		color = new float[12];

	}

	public void flatten() {
		z0 = z1 = z2 = -1.2f;
	}
	
	public void init() {
		vertices[0] = x0;
		vertices[1] = y0;
		vertices[2] = z0;
		vertices[3] = x1;
		vertices[4] = y1;
		vertices[5] = z1;
		vertices[6] = x2;
		vertices[7] = y2;
		vertices[8] = z2;
		color[0] = (float) r / 255.0f;
		color[1] = (float) g / 255.0f;
		color[2] = (float) b / 255.0f;
		color[3] = (float) a / 255.0f;

		color[4] = (float) r / 255.0f;
		color[5] = (float) g / 255.0f;
		color[6] = (float) b / 255.0f;
		color[7] = (float) a / 255.0f;

		
		color[8] = (float) r / 255.0f;
		color[9] = (float) g / 255.0f;
		color[10] = (float) b / 255.0f;
		color[11] = (float) a / 255.0f;
		
		
		ByteBuffer byteBuf = ByteBuffer.allocateDirect(vertices.length * 4);
		byteBuf.order(ByteOrder.nativeOrder());
		vertexBuffer = byteBuf.asFloatBuffer();
		vertexBuffer.put(vertices);
		vertexBuffer.position(0);
		
		byteBuf = ByteBuffer.allocateDirect( color.length * 4);
		byteBuf.order(ByteOrder.nativeOrder());
		colorBuffer = byteBuf.asFloatBuffer();
		colorBuffer.put( color );
		colorBuffer.position(0);
		
	}

	public void draw(GL10 gl) {
		gl.glColor4f(color[0], color[1], color[2], color[3]);
		gl.glFrontFace(GL10.GL_CW);
		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertices.length / 3);
		gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
	}

	@Override
	public float[] getVertexData() {
		return vertices;
	}

	@Override
	public float[] getColorData() {
		return color;
	}


//	public void drawGLES2(int vertexHandle, int colorHandle ) {
//		GLES20.glVertexAttribPointer( vertexHandle, vertices.length / 3, GLES20.GL_FLOAT, false, 0, vertexBuffer );
//		GLES20.glEnableVertexAttribArray( vertexHandle );
//		
//		GLES20.glVertexAttribPointer( colorHandle, 4, GLES20.GL_FLOAT, false, 0, colorBuffer );
//		GLES20.glEnableVertexAttribArray( colorHandle );
//		
//		GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 3 );
//	}

}
